package Jump;

import java.awt.*;
import java.util.Random;

public class Obstacle {
    public int x;
    public int y;
    public int width;
    public int height;
    public int type; // 0: 仙人掌, 1: 鸟, 2: 石头
    public int speed;
    public static final int GROUND_HEIGHT = 80;
    public static final Random random = new Random();

    public Obstacle(int initialSpeed) {
        this.x = JumpPanel.WIDTH;
        this.type = random.nextInt(3); // 随机选择障碍物类型 (0,1,2)
        this.speed = initialSpeed;
        if (type == 0) { // 仙人掌
            this.width = 20;
            this.height = 30 + random.nextInt(30);
            this.y = JumpPanel.HEIGHT - GROUND_HEIGHT - height;
        } else if (type == 1) { // 鸟
            this.width = 30;
            this.height = 20;
            this.y = JumpPanel.HEIGHT - GROUND_HEIGHT -20- height - random.nextInt(50);
        } else { // 石头
            this.width = 40 + random.nextInt(20);
            this.height = 25 + random.nextInt(15);
            this.y = JumpPanel.HEIGHT - GROUND_HEIGHT - height;
        }
    }

    public void update(int score) {
        x -= speed + score / 500; // 随着分数增加速度
    }

    public void draw(Graphics g) {
        if (type == 0) { // 仙人掌
            g.setColor(new Color(34, 139, 34));
            g.fillRect(x, y, width, height);
            // 添加一些分支
            g.fillRect(x - 5, y + 10, 5, 5);
            g.fillRect(x + width, y + 15, 5, 5);
        } else if (type == 1) { // 鸟
            g.setColor(new Color(160, 82, 45));
            // 身体
            g.fillOval(x, y, width, height);
            g.setColor(new Color(196, 173, 40, 255));
            // 嘴
            int[] xPoints = {x + 3, x -7, x + 3};
            int[] yPoints = {y + 5, y + 10, y + 15};
            g.fillPolygon(xPoints, yPoints, 3);
            // 翅膀
            g.setColor(Color.WHITE);
            g.fillOval(x + 20, y + 5, 4, 4);
        } else { // 石头
            g.setColor(new Color(105, 105, 105));
            g.fillOval(x, y, width, height);
            // 添加一些纹理
            g.setColor(new Color(139, 137, 137));
            g.fillOval(x + 15, y + 5, 4, 4);
            g.fillOval(x + 25, y + 10, 6, 3);
        }
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, width, height);
    }
}